package com.alpha.core.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * 统一实体公共基类
 *
 * @author chenruwan
 * @since 2022-01-01
 */
@Data
@Accessors(chain = true)
@Schema(name = "实体基类")
public class AlphaEntity {
    @TableId
    @Schema(description = "唯一 Id")
    private Long id;

    @TableLogic
    @Schema(description = "是否已删除")
    private Integer deleted;

    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "创建人")
    private String createdBy;

    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "创建时间(yyyy-MM-dd HH:mm:ss)", example = "2022-01-01 00:00:00")
    private Date createdAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @Schema(description = "更新人")
    private String updatedBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "更新时间(yyyy-MM-dd HH:mm:ss)", example = "2022-01-01 00:00:00")
    private Date updatedAt;

    public static boolean isEffectiveId(Long id) {
        return id != null && id > 0;
    }

    /**
     * 链式调用时，转换成子类类型，仿builder
     */
    @SuppressWarnings("unchecked")
    public <T> T build() {
        return (T) this;
    }
}
